package com.dys.hejiayuncommunity.web.controller.system;

import com.dys.hejiayuncommunity.common.constant.Constants;
import com.dys.hejiayuncommunity.common.constant.UserConstants;
import com.dys.hejiayuncommunity.common.core.controller.BaseController;
import com.dys.hejiayuncommunity.common.core.domain.BaseResponse;
import com.dys.hejiayuncommunity.common.utils.ChainedMap;
import com.dys.hejiayuncommunity.common.utils.SecurityUtils;
import com.dys.hejiayuncommunity.common.utils.ServletUtils;
import com.dys.hejiayuncommunity.system.domain.LoginUser;
import com.dys.hejiayuncommunity.system.domain.SysMenu;
import com.dys.hejiayuncommunity.system.service.SysMenuService;
import com.dys.hejiayuncommunity.system.service.TokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 疾风
 * @ClassName: SysMenuController
 * @Description: 菜单相关信息
 * @CreateDate: 2024/12/31
 * @Version: v1.0.0
 */
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取菜单列表
     */
    @GetMapping(value = {"/list"})
    public BaseResponse list(SysMenu menu) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getSysUser().getUserId();
        List<SysMenu> menuList = menuService.selectMenuList(menu, userId);
        return BaseResponse.success(menuList);
    }

    /**
     * 根据菜单编号获取菜单详细信息
     */
    @GetMapping(value = "/{menuId}")
    public BaseResponse getInfo(@PathVariable(value = "menuId") Long menuId) {
        return BaseResponse.success(menuService.selectMenuById(menuId));
    }

    /**
     * 新增菜单
     */
    @PostMapping
    public BaseResponse add(@RequestBody SysMenu menu) {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return BaseResponse.failed("新增菜单失败," + menu.getMenuName() + "已经存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) {
            return BaseResponse.failed("新增菜单失败," + menu.getPath() + "地址必须以http(s)://开头");
        }
        menu.setCreateBy(SecurityUtils.getUserName());
        return super.toAjax(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单信息
     */
    @PutMapping
    public BaseResponse edit(@Validated @RequestBody SysMenu menu) {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return BaseResponse.failed("新增菜单失败," + menu.getMenuName() + "菜单名称已经存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame()) && !StringUtils.startsWithAny(menu.getPath(), Constants.HTTP, Constants.HTTPS)) {
            return BaseResponse.failed("新增菜单失败," + menu.getPath() + "地址必须以http(s)://开头");
        } else if (menu.getMenuId().equals(menu.getParentId())) {
            return BaseResponse.failed("新增菜单失败," + menu.getMenuName() + "上级菜单不能选择自己");
        }
        menu.setUpdateBy(SecurityUtils.getUserName());
        return super.toAjax(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/{menuId}")
    public BaseResponse remove(@PathVariable(value = "menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return BaseResponse.success("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return BaseResponse.success("菜单已经分配,不允许删除");
        }
        return super.toAjax(menuService.deleteMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public BaseResponse treeSelect(SysMenu menu) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getSysUser().getUserId();
        List<SysMenu> menuList = menuService.selectMenuList(menu, userId);
        return BaseResponse.success(menuService.buildMenuTreeSelect(menuList));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeSelect/{roleId}")
    public ChainedMap roleMenuTreeSelect(@PathVariable(value = "roleId") Long roleId) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        List<SysMenu> menuList = menuService.selectMenuList(loginUser.getSysUser().getUserId());

        ChainedMap map = ChainedMap.create().set("code", 200).set("msg", "操作成功");
        // 获取当前角色所拥有的所有菜单权限Id
        map.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        // 获取菜单树
        map.put("menus", menuService.buildMenuTreeSelect(menuList));

        return map;
    }
}
